<template>
  <div class="review-manage-container">
    <el-card class="review-manage-card">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="搜索">
            <el-input
              v-model="searchForm.keyword"
              placeholder="订单号/用户名/评价内容"
              clearable
              @keyup.enter="handleSearch"
            />
          </el-form-item>
          <el-form-item label="评分">
            <el-select
              class="rating-select"
              v-model="searchForm.rating"
              placeholder="选择评分"
              clearable
            >
              <el-option label="1星" :value="1" />
              <el-option label="2星" :value="2" />
              <el-option label="3星" :value="3" />
              <el-option label="4星" :value="4" />
              <el-option label="5星" :value="5" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 评价列表 -->
      <el-table v-loading="loading" :data="reviewList" style="width: 100%" border>
        <el-table-column label="订单信息" min-width="180">
          <template #default="{ row }">
            <div class="order-info">
              <div class="order-number">订单号: {{ row.order.order_number }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column v-if="userRole != 'user'" label="用户信息" width="180">
          <template #default="{ row }">
            <div class="user-info">
              <el-avatar
                :size="32"
                :src="row.customer.avatar"
                :icon="row.is_anonymous ? UserFilled : null"
              />
              <div class="user-detail">
                <span class="username">{{
                  row.is_anonymous ? '匿名用户' : row.customer.nickname || row.customer.username
                }}</span>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="评分" width="120">
          <template #default="{ row }">
            <el-rate v-model="row.rating" disabled show-score />
          </template>
        </el-table-column>

        <el-table-column label="评价内容" min-width="300">
          <template #default="{ row }">
            <div class="review-content">
              <div class="content">{{ row.content }}</div>
              <div v-if="row.photos?.length" class="photos">
                <el-image
                  v-for="(photo, index) in row.photos"
                  :key="index"
                  :src="photo"
                  fit="cover"
                  class="photo"
                />
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="摄影师" width="120">
          <template #default="{ row }">
            <div class="user-info">
              <el-avatar :size="32" :src="row.photographer.avatar" />
              <div class="user-detail">
                <span class="username">{{ row.photographer.nickname }}</span>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="评价时间" width="180">
          <template #default="{ row }">
            {{ row.created_at }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button
              v-if="userRole === 'super_admin' || userRole === 'admin' || userRole === 'user'"
              type="danger"
              link
              @click="handleDelete(row)"
            >
              删除评价
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { UserFilled } from '@element-plus/icons-vue'
import { getReviews, deleteReview } from '@/api/review'
import type { Review } from '@/types/review'
import { useUserStore } from '@/stores/user'

const userStore = useUserStore()
const userRole = computed(() => userStore.userRole)

const loading = ref(false)
const reviewList = ref<Review[]>([])

// 搜索表单
const searchForm = ref({
  keyword: '',
  rating: undefined as number | undefined,
})

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  fetchData()
}

// 重置搜索
const resetSearch = () => {
  searchForm.value = {
    keyword: '',
    rating: undefined,
  }
  handleSearch()
}

// 分页大小改变
const handleSizeChange = (val: number) => {
  pageSize.value = val
  fetchData()
}

// 当前页改变
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  fetchData()
}

// 获取数据
const fetchData = async () => {
  loading.value = true
  try {
    const { data } = await getReviews({
      keyword: searchForm.value.keyword,
      rating: searchForm.value.rating,
      page: currentPage.value,
      pageSize: pageSize.value,
    })
    reviewList.value = data.data.items
    total.value = data.data.meta.total
  } catch (error) {
    ElMessage.error('获取数据失败')
  } finally {
    loading.value = false
  }
}

// 删除评价
const handleDelete = async (row: Review) => {
  try {
    await ElMessageBox.confirm('确定要删除该评价吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })

    await deleteReview(row.order_id)
    ElMessage.success('删除成功')
    fetchData()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.review-manage-container {
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
  min-height: calc(100vh - 200px);
  box-sizing: border-box;
}

.review-manage-card {
  margin-top: 0;
}

.search-bar {
  margin-bottom: 20px;
}

.order-info {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.package-name {
  color: #666;
  font-size: 13px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-detail {
  display: flex;
  flex-direction: column;
}

.username {
  font-size: 14px;
  color: #333;
}

.review-content {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.photos {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.photo {
  width: 60px;
  height: 60px;
  border-radius: 4px;
  cursor: pointer;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.rating-select {
  width: 100px;
}
</style>
